<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="登录账号" prop="Q_userName_S_LK">
            <el-input v-model="queryData.Q_userName_S_LK" placeholder="登录账号" />
          </el-form-item>
          <el-form-item label="登录城市" prop="Q_city_S_LK">
            <el-input v-model="queryData.Q_city_S_LK" placeholder="城市" />
          </el-form-item>
          <el-form-item label="类型" prop="Q_type_S_EQ">
            <el-select v-model="queryData.Q_type_S_EQ" placeholder="类型">
              <el-option
                v-for="item in optionOfType"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="登陆时间">
            <el-col :span="12">
              <el-date-picker v-model="queryData.Q_operaTime_D_GE" class="width-130" type="date" value-format="yyyy-MM-dd" />
            </el-col>
            <el-col :span="2" class="line">-</el-col>
            <el-col :span="10">
              <el-date-picker v-model="queryData.Q_operaTime_D_LE" class="width-130" type="date"value-format="yyyy-MM-dd" />
            </el-col>
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <el-col :span="1.5">
            <el-button v-permission="['log:login:delete']" v-if="tableData.length > 0" size="mini" plain type="warning" icon="el-icon-delete" @click="handleDeleteAll()">
              清空
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-permission="['log:login:delete']" v-if="selectionData.length > 0" size="mini" plain type="danger" icon="el-icon-delete" @click="handleDelete()">
              删除
            </el-button>
          </el-col>
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="logId"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column align="center" label="登录账号" prop="userName" width="100" show-overflow-tooltip />
          <el-table-column align="center" label="登录城市" prop="city" sortable width="160" show-overflow-tooltip />
          <el-table-column align="center" label="登录地点" prop="area" width="150" show-overflow-tooltip />
          <el-table-column align="center" label="IP" prop="ip" sortable width="180" show-overflow-tooltip />
          <el-table-column align="center" label="浏览器" prop="browser" width="160" show-overflow-tooltip />
          <el-table-column align="center" label="操作系统" prop="os" sortable width="150" show-overflow-tooltip />
          <el-table-column align="center" label="类型" prop="type" sortable width="110">
            <template slot-scope="scope">
              <el-tag :type="getDictType('type', scope.row.type)" v-text="getDictValue('type', scope.row.type)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="结果" prop="status" sortable width="110">
            <template slot-scope="scope">
              <el-tag :type="getDictType('status', scope.row.status)" v-text="getDictValue('status', scope.row.status)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="描述" prop="desc">
            <template slot-scope="scope">
              <span style="cursor: pointer" @click="handleInfo(scope.row)">{{ scope.row.desc | ellipsis }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="访问时间" prop="operaTime" width="160" show-overflow-tooltip />
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>
    <el-dialog v-el-drag-dialog title="日志信息" :visible.sync="showDetail" :append-to-body="true">
      <el-card>
        <el-row class="m-b-20">
          <el-col :span="3">
            <span style="font-weight: bold">登录账号</span>
          </el-col>
          <el-col :span="5">
            {{ detail.userName }}
          </el-col>
          <el-col :span="3">
            <span style="font-weight: bold">类型</span>
          </el-col>
          <el-col :span="5">
            {{ detail.type }}
          </el-col>
          <el-col :span="3">
            <span style="font-weight: bold">结果</span>
          </el-col>
          <el-col :span="5">
            {{ detail.status }}
          </el-col>
        </el-row>
        <el-row class="m-b-20">
          <el-col :span="3">
            <span style="font-weight: bold">登录城市</span>
          </el-col>
          <el-col :span="5">
            {{ detail.city }}
          </el-col>
          <el-col :span="3">
            <span style="font-weight: bold">登录地点</span>
          </el-col>
          <el-col :span="5">
            {{ detail.area }}
          </el-col>
          <el-col :span="3">
            <span style="font-weight: bold">IP</span>
          </el-col>
          <el-col :span="5">
            {{ detail.ip }}
          </el-col>
        </el-row>
        <el-row>
          <el-table :data="detail.query" stripe style="width: 100%">
            <el-table-column align="center" label="参数名" prop="name" show-overflow-tooltip />
            <el-table-column align="center" label="参数值" prop="value" show-overflow-tooltip />
          </el-table>
        </el-row>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { loginLogTable, loginLogDelete } from '@/api/system/log'

export default {
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_userName_S_LK: undefined,
        Q_city_S_LK: undefined,
        Q_type_S_EQ: '',
        Q_operaTime_D_GE: undefined,
        Q_operaTime_D_LE: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: 'operaTime',
        order: 'desc'
      },
      selectionData: [],
      optionOfType: [],
      optionOfStatus: [],
      detail: {
        query: []
      },
      showDetail: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getDictData()
      this.getTableData()
    },
    getDictData() {
      this.$dictGroup('sys_login_type').then(response => {
        this.optionOfType = response
      }).catch(e => {})
      this.$dictGroup('sys_message_send_status').then(response => {
        this.optionOfStatus = response
      }).catch(e => {})
    },
    getDictType(property, value) {
      if ('type' === property) {
        return this.$api.getDictElement(this.optionOfType, value)[2]
      } else if ('status' === property) {
        return this.$api.getDictElement(this.optionOfStatus, value)[2]
      }
    },
    getDictValue(property, value) {
      if ('type' === property) {
        return this.$api.getDictElement(this.optionOfType, value)[1]
      } else if ('status' === property) {
        return this.$api.getDictElement(this.optionOfStatus, value)[1]
      }
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      loginLogTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.queryTable.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(row) {
      let query = []

      if (row.desc) {
        let param = row.desc.split('&')
        for (let p in param) {
          if (param[p].indexOf('=') !== -1) {
            let name = param[p].split('=')[0]
            let value = param[p].split('=')[1]
            query.push({
              name: name,
              value: value ? value : ''
            })
          } else {
            query.push({
              name: p,
              value: param[p]
            })
          }
        }
      }

      this.detail = {
        userName: row.userName,
        type: this.getDictValue('type', row.type),
        status: this.getDictValue('status', row.status),
        city: row.city,
        area: row.area,
        ip: row.ip,
        query: query
      }

      this.showDetail = true
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function(row, index) {
        return row.logId
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        loginLogDelete({ ids: id }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    },
    handleDeleteAll() {
      this.$confirm('是否清空数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        loginLogDelete({}).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>
